// pages/home/searchList.js
const service = require('./searchListService.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataSource: [],
    startPage: 0,
    nextPage: 1,
    searchKey:"",
    hasNextPage: false,
    loadMoreData: '加载更多……',
    shareMsg:"",
    isLoading: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.list = this.selectComponent("#list");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  startSearch: function(e){
    let self = this;
    self.setData({
      searchKey: e.detail.value,
      isLoading: true,
    });

    let param = {
      "searchKey": e.detail.value,
      "startPage": self.data.startPage
    }    
    self.requestData(false,param)
  },
  requestData: function (addMore,param){
    let self = this;
    service.searchGoods(param, function (data) {
      console.log(data)
      var source = [];
      if (addMore == true){
        source = self.data.dataSource.concat(data.items)
      }else{
        source = data.items
      }
      self.setData({
        dataSource: source,
        nextPage: data.nextPage,
        hasNextPage: data.hasNextPage,
        isLoading: false,
      });
    });
  },
  /**
   * view触底监听
   */
  onReachBottom: function () {
    // 当前页是最后一页
    if (!this.data.hasNextPage) {
      this.setData({
        loadMoreData: '暂无更多数据'
      })
      return;
    }
    let param = {
      "searchKey": this.data.searchKey,
      "startPage": this.data.nextPage
    }  
    this.setData({
      startPage: this.data.nextPage
    })
    this.requestData(true,param)
  },
})